package exemple15;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import exemple15.javabeans.Client;
import exemple15.modeles.ModeleClientDAO;


@SuppressWarnings("serial")
public class ClientAction extends ActionSupport implements Preparable, ModelDriven{
	
    private Client client;
    private List<Client> listeClients;
    private int idClientEnCours;
    
    public void prepare() throws Exception {
    	ModeleClientDAO modeleClientDAO=new ModeleClientDAO();
    	// en creation, creer un nouvel objet vide
    	if(idClientEnCours==0) 
    	{
    		client=new Client();
    	} 
    	// en modification, retourner les infos de l'objet
    	else
    	{
    		client=modeleClientDAO.getClient(idClientEnCours);
    	}
    }
    
    public Object getModel() {
    	return client;
    }
    
	public int getIdClientEnCours() {
		return idClientEnCours;
	}

	public void setIdClientEnCours(int idClientEnCours) {
		this.idClientEnCours = idClientEnCours;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public List<Client> getListeClients() {
		ModeleClientDAO modeleClientDAO=new ModeleClientDAO();
		listeClients=(ArrayList<Client>)modeleClientDAO.getListeClient();
		return listeClients;
	}

	public void setListeClients(List<Client> listeClients) {
		this.listeClients = listeClients;
	}
	
	// retourner la liste des clients apres recuperation
	public String lister() 
	{
		ModeleClientDAO modeleClientDAO=new ModeleClientDAO();
		listeClients=(ArrayList<Client>)modeleClientDAO.getListeClient();
        return SUCCESS;
    }

	// ajouter le client dans le modele
	public String ajouter()
	{	
		ModeleClientDAO modeleClientDAO=new ModeleClientDAO();
		modeleClientDAO.ajouterClient(client);
	    return SUCCESS;
	}
	
	// afficher le formulaire en edition
	public String editer()
	{
		 return SUCCESS;
	}
	
	// modifier un client
	public String modifier()
	{
		ModeleClientDAO modeleClientDAO=new ModeleClientDAO();
		modeleClientDAO.modifierClient(client);
		return SUCCESS;
	}
		
	// supprimer un client a partir du parametre recu nomme idClient
	public String supprimer()
	{
		ModeleClientDAO modeleClientDAO=new ModeleClientDAO();
		modeleClientDAO.supprimerClient(idClientEnCours);
		return SUCCESS;
	}
	
	
}


